//
//  ViewController.m
//  03基本动画介绍
//
//  Created by apple on 15-6-12.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, weak) CALayer *myLayer;
@end

@implementation ViewController


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    if ([self.myLayer animationForKey:@"anim1"] == nil) {
//       
//    }
    
    
    if ([self.myLayer animationForKey:@"anim1"]) {
        return;
    }
    
    self.myLayer.bounds = CGRectMake(0, 0, 100, 100);
    
    
    
    // 实现抖动效果
    // 1. 创建一个核心动画对象
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    
    anim.values = @[@(-M_PI_4 * 0.2), @(M_PI_4 * 0.2), @(-M_PI_4 * 0.2)];
    
    
    anim.repeatCount = CGFLOAT_MAX;
    
    //anim.duration = 2;
    anim.duration = 0.1;
    
    
    
    // 设置动画执行完毕以后不要回去
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    
    
    // 3. 把核心动画对象添加到对应的CALayer里面
    [self.myLayer addAnimation:anim forKey:@"anim1"];
    
    
    
   
}


// 组动画
- (void)test5
{
    // 组动画
    CAAnimationGroup *animGroup = [[CAAnimationGroup alloc] init];
    
    
    //1.
    // 1. 创建一个核心动画对象
    CABasicAnimation *anim = [[CABasicAnimation alloc] init];
    // 1.2 设置核心动画要执行的动画操作（设置动画属性）
    anim.keyPath = @"position";
    anim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(0, 500)];
    
    
    
    // 2.
    // 1. 创建一个核心动画对象
    CABasicAnimation *anim1 = [[CABasicAnimation alloc] init];
    // 2. 设置核心动画要执行的动画操作（设置动画属性）
    anim1.keyPath = @"transform.rotation";
    anim1.toValue = @(M_PI * 20);
    
    
    
    // 3.
    // 创建关键帧动画对象
    CAKeyframeAnimation *anim2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 300, 100)];
    // 设置path
    anim2.path = path.CGPath;
    
    
    // 4.
    CABasicAnimation *anim3 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    anim3.toValue = @(0.3);
    
    
    animGroup.animations = @[anim, anim1, anim2, anim3];
    
    animGroup.duration = 3;
    animGroup.repeatCount = CGFLOAT_MAX;
    
    //animGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    //animGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    animGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    
    [self.myLayer addAnimation:animGroup forKey:nil];
}


// 关键帧动画
- (void)test4
{
    // 创建关键帧动画对象
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    anim.duration = 3;
    
    
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 150, 100)];
    
    // 设置path
    anim.path = path.CGPath;
    
    
    //
    [self.myLayer addAnimation:anim forKey:nil];
}


// 关键帧动画
- (void)test3
{
    // 创建关键帧动画对象
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    anim.duration = 3;
    
    // 设置属性值
    NSValue *p1 = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
    
    NSValue *p2 = [NSValue valueWithCGPoint:CGPointMake(150, 50)];
    
    NSValue *p3 = [NSValue valueWithCGPoint:CGPointMake(50, 150)];
    
    NSValue *p4 = [NSValue valueWithCGPoint:CGPointMake(150, 150)];
    anim.values = @[p1, p2, p3, p4];
    
    
    //
    [self.myLayer addAnimation:anim forKey:nil];
}


// 基本动画
- (void)test2
{
    // 1. 创建一个核心动画对象
    CABasicAnimation *anim = [[CABasicAnimation alloc] init];
    
    // 2. 设置核心动画要执行的动画操作（设置动画属性）
    anim.keyPath = @"transform.rotation";
    //anim.fromValue = @(0);
    anim.toValue = @(M_PI * 2);
    //anim.byValue = @(M_PI_4 * 0.5);
    
    
    anim.repeatCount = CGFLOAT_MAX;
    
    anim.duration = 2;
    
    
    
    // 设置动画执行完毕以后不要回去
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    
    
    // 3. 把核心动画对象添加到对应的CALayer里面
    [self.myLayer addAnimation:anim forKey:nil];

}


// 基本动画
- (void)test
{
    // 1. 创建一个核心动画对象
    CABasicAnimation *anim = [[CABasicAnimation alloc] init];
    
    // 2. 设置核心动画要执行的动画操作（设置动画属性）
    anim.keyPath = @"position";
    anim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(0, 500)];
    
    // 设置动画执行完毕以后不要回去
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    
    
    // 监听动画执行完毕的事件(隐式代理)
    anim.delegate = self;
    
    // 3. 把核心动画对象添加到对应的CALayer里面
    [self.myLayer addAnimation:anim forKey:nil];
}


//----------- 动画代理对象的两个方法 --------------
- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"--- 动画启动了 ----");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    NSLog(@"----- 动画结束了-----");
    self.myLayer.position = CGPointMake(0, 500);
    NSLog(@"%@", NSStringFromCGPoint(self.myLayer.position));
   //
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CALayer *layer = [[CALayer alloc] init];
    layer.backgroundColor = [UIColor redColor].CGColor;
    layer.bounds = CGRectMake(0, 0, 100, 100);
    layer.position = CGPointMake(150, 150);
    [self.view.layer addSublayer:layer];
    self.myLayer = layer;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
